<template>
    <div class="upload-button">
        <Upload style="max-width:300px" :show-upload-list="true" :default-file-list="list" action="" :on-remove="on_remove" :max-size="1024*100" :before-upload="before_upload" :on-format-error="on_format_error" :on-error="on_error" :on-exceeded-size="on_exceeded_size">
            <Button type="info" ghost icon="ios-cloud-upload-outline" class="d-block mb-1">上传</Button>
        </Upload>
        <div class="font-12 mt-1">
            <span class="mr-1">
                <Icon type="md-cube" size="14" />
                模型文件
            </span>
            <span class="mr-1">
                <Icon type="logo-youtube" size="14" />
                视频文件
            </span>
            <span>
                <Icon type="md-document" size="14"/>
                图片或文档
            </span>
        </div>
    </div>
</template>

<script>
import {deep_clone} from "@/common/tool"
export default {
    props:['file_list'],
    data(){
        return {
            list:[]
        }
    },
    methods:{
        before_upload(file){
            const type=localStorage.getItem('currentIdentity')
            const {upload}=require(`@/common/${type}api`)
            const data = new FormData();
            data.append("file", file);
            data.append(type+"id", localStorage.getItem(type+'id'));
            upload(data)
            .then(res=>{
                if(res){
                    this.list.push(res.data.info)
                    this.$emit('set_files',this.list.map(el=>el.id).join(','))
                }
            })
            return false
        },
        on_format_error(){
            this.$Notice.error({
                title: '失败',
                desc: '文件格式不正确！'
            });
        },
        on_error(){
            this.$Notice.error({
                title: '失败',
                desc: '文件上传失败'
            });
        },
        on_exceeded_size(){
            this.$Notice.error({
                title: '失败',
                desc: '文件太大了，只允许上传100M以内的！'
            });
        },
        on_remove(file,list){
            this.list=list
            this.$emit('set_files',this.list.map(el=>el.id).join(','))
        }
    },
    mounted(){
        if(this.file_list){
            this.list=deep_clone(this.file_list)
        }
    }
}
</script>